/** 
 * @Title: BBSSyn.java 
 * @Package com.zizo.autohome.word.syn.dao 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月13日 下午5:05:40 
 */
package com.zizo.autohome.word.syn.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zizo.util.Analysis;
import com.zizo.util.ConnectionPool;

/**
 * @ClassName: BBSSyn
 * @Description: TODO 近义词替换
 * @author HeYan
 * @date 2016年9月13日 下午5:05:40
 * 
 */
public class BBSSynDao implements Runnable {
	private Connection conn;
	private String id;// tableName
	private Map<String, String> sys;

	/**
	 * @param conn
	 * @param id
	 * @param sys
	 */
	public BBSSynDao(Connection conn, String id, Map<String, String> sys) {
		super();
		this.conn = conn;
		this.id = id;
		this.sys = sys;
	}

	public void run() {
		getBBSData();
		insertFinTable(id);
		System.out.println(id);
	}

	public final void getBBSData() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bbs_bbsid,bbs_title,bbs_content FROM autohome_bbs." + id;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int bbs_id = rs.getInt("bbs_bbsid");
				String bbs_bt = rs.getString("bbs_title");
				String bbs_nr = rs.getString("bbs_content");
				String nr = "";
				String bt = "";
				if (bbs_bt != null) {
					bt = Analysis.replaceSyn(bbs_bt, sys);
				}
				if (bbs_nr != null) {
					nr = Analysis.replaceSyn(bbs_nr, sys);
				}
				updateData(bbs_id, bt, nr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public final void updateData(int bbs_id, String bt, String nr) {
		PreparedStatement ps = null;
		String sql = "UPDATE " + id + " SET bbs_title=?,bbs_content=? WHERE bbs_bbsid=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, bt);
			ps.setString(2, nr);
			ps.setInt(3, bbs_id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Title: getSyn
	 * @Description: 查找carvoice_autohome中topic_syn表中的数据对应，返回Map表，用于同义词的替换
	 * @return Map<String,String>
	 * @author HeYan
	 */
	public Map<String, String> getSyn() {
		Map<String, String> map = new HashMap<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "select syn_word,syn_syn from carvoice_autohome.topic_syn";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				map.put(rs.getString("syn_word"), rs.getString("syn_syn"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

	public List<String> showTables() {
		List<String> list = new ArrayList<>();
		String sql = "SHOW TABLES FROM autohome_syn LIKE 'bbs_%';";
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<String> getFinTables() {
		List<String> list = new ArrayList<>();
		String sql = "SELECT table_fin.`table` FROM table_fin";
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void insertFinTable(String name) {
		Statement st = null;
		String sql = "INSERT INTO autohome_syn.table_fin(`table`) VALUES('" + name + "')";
		try {
			st = conn.createStatement();
			st.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param conn
	 */
	public BBSSynDao(Connection conn) {
		super();
		this.conn = conn;
	}

	public static void main(String[] args) {
		ExecutorService fix = Executors.newFixedThreadPool(15);
		Connection conn = ConnectionPool.getConnection("autohome_syn", "localhost");
		BBSSynDao dao = new BBSSynDao(conn);
		Map<String, String> syn = dao.getSyn();
		List<String> tables = dao.showTables();
		tables.removeAll(dao.getFinTables());
		for (String s : tables) {
			fix.execute(new BBSSynDao(conn, s, syn));
		}
		fix.shutdown();
	}
}
